package com.lovo.bbs.bo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.naming.NamingException;

import com.lovo.bbs.dao.ForumDao;
import com.lovo.bbs.po.ForumPo;
import com.lovo.bbs.vo.ForumVo;

/**
 * 论坛Bo
 * 
 * @author tiancen2001
 * 
 */
public class ForumBo {
	// 操作类型
	public static final int OPER_OF_INSERT = 1;
	public static final int OPER_OF_UPDATE = 2;
	public static final int OPER_OF_DELETE = 3;

	/**
	 * 返回所有论坛Vo列表
	 * 
	 * @author tiancen2001
	 * 
	 */
	public  ArrayList<ForumVo> getAllForum() {
		ArrayList<ForumPo> forumPos = null;
		try {
			forumPos = new ForumDao().getAllForum();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		ArrayList<ForumVo> forumVos = new ArrayList<ForumVo>();
		Iterator<ForumPo> itPo = forumPos.iterator();
		while (itPo.hasNext()) {
			ForumPo tempPo = itPo.next();
			ForumVo tempVo = new ForumVo();

			tempVo.setForumID(tempPo.getForumID());
			tempVo.setBoardID(tempPo.getBoardID());

			tempVo.setForumName(tempPo.getForumName());
			tempVo.setAboutForum(tempPo.getAboutForum());

			tempVo.setTopicNum(tempPo.getTopicNum());
			tempVo.setPostNum(tempPo.getPostNum());
			tempVo.setTodayTopicNum(tempPo.getTodayTopicNum());

			tempVo.setLastTopicID(tempPo.getLastTopicID());
			tempVo.setLastTopicTitle(tempPo.getLastTopicTitle());
			tempVo.setLastTopicAuthor(tempPo.getLastTopicAuthor());
			tempVo.setLastTopicDate(tempPo.getLastTopicDate());

			forumVos.add(tempVo);
		}
		return forumVos;
	}

	/**
	 * 根据论坛Vo列表生成Map
	 * 
	 * @return
	 */
	public  HashMap<Integer, ForumVo> getForumMap(
			ArrayList<ForumVo> forumVos) {
		HashMap<Integer, ForumVo> forumMap = new HashMap<Integer, ForumVo>();
		Iterator<ForumVo> it = forumVos.iterator();
		while (it.hasNext()) {
			ForumVo vo = (ForumVo) it.next();
			forumMap.put(vo.getForumID(), vo);
		}
		return forumMap;
	}

	/**
	 * 新增一个主题
	 * 
	 * @param topicid
	 * @return
	 */
	public  int addOneTopic(int topicid, int forumid) {
		int setted = 0;
		try {
			setted =  new ForumDao().addOneTopic(topicid, forumid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return setted;
	}

	/**
	 * 新增一个回帖
	 * 
	 * @param forumid
	 * @return
	 */
	public  int addOnePost(int forumid) {
		int added = 0;
		try {
			added = new ForumDao().addOnePost(forumid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return added;
	}

	/**
	 * 分别计算各个论坛的今日主题数
	 * 
	 * @return
	 */
	public  void calcTodayTopicNum() {
		try {
			new ForumDao().calcTodayTopicNum();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 返回指定板块Id下的论坛
	 */
	public  ArrayList<ForumVo> getForumByBoardID(int boardid) {
		ArrayList<ForumPo> forumPos = null;
		try {
			forumPos = new ForumDao().getForumByBoardID(boardid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		ArrayList<ForumVo> forumVos = new ArrayList<ForumVo>();
		Iterator<ForumPo> itPo = forumPos.iterator();
		while (itPo.hasNext()) {
			ForumPo tempPo = itPo.next();
			ForumVo tempVo = new ForumVo();

			tempVo.setForumID(tempPo.getForumID());
			tempVo.setBoardID(tempPo.getBoardID());

			tempVo.setForumName(tempPo.getForumName());
			tempVo.setAboutForum(tempPo.getAboutForum());

			tempVo.setTopicNum(tempPo.getTopicNum());
			tempVo.setPostNum(tempPo.getPostNum());
			tempVo.setTodayTopicNum(tempPo.getTodayTopicNum());

			tempVo.setLastTopicID(tempPo.getLastTopicID());
			tempVo.setLastTopicTitle(tempPo.getLastTopicTitle());
			tempVo.setLastTopicAuthor(tempPo.getLastTopicAuthor());
			tempVo.setLastTopicDate(tempPo.getLastTopicDate());

			forumVos.add(tempVo);
		}
		return forumVos;
	}

	/**
	 * 返回指定论坛ID的论坛
	 */
	public  ForumVo getForumByForumID(int forumid) {
		ForumVo tempVo = new ForumVo();
		ForumPo tempPo = null;
		try {
			tempPo = new ForumDao().getForumByForumID(forumid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		tempVo.setForumID(tempPo.getForumID());
		tempVo.setBoardID(tempPo.getBoardID());

		tempVo.setForumName(tempPo.getForumName());
		tempVo.setAboutForum(tempPo.getAboutForum());

		tempVo.setTopicNum(tempPo.getTopicNum());
		tempVo.setPostNum(tempPo.getPostNum());
		tempVo.setTodayTopicNum(tempPo.getTodayTopicNum());

		tempVo.setLastTopicID(tempPo.getLastTopicID());
		tempVo.setLastTopicTitle(tempPo.getLastTopicTitle());
		tempVo.setLastTopicAuthor(tempPo.getLastTopicAuthor());
		tempVo.setLastTopicDate(tempPo.getLastTopicDate());
		return tempVo;
	}

	/**
	 * 论坛更新
	 */
	public  int updateForum(ForumVo vo, int operType) {
		int changed = 0;
		ForumPo po = new ForumPo();
		po.setForumID(vo.getForumID());
		po.setBoardID(vo.getBoardID());
		po.setForumName(vo.getForumName());
		po.setAboutForum(vo.getAboutForum());
		try {
			changed = new ForumDao().updateForum(po, operType);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return changed;
	}

	/**
	 * 刷新数据表
	 * 
	 * @param forumid
	 * @return
	 */
	public  void reset(int forumid) {
		calcTopicNum(forumid);// 主题数
		calcPostNum(forumid);// 回复数
		calcLastTopic(forumid);// 最后主题
	}

	/**
	 * 计算指定论坛的主题数
	 */
	public  void calcTopicNum(int forumid) {
		try {
			new ForumDao().calcTopicNum(forumid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 计算指定论坛的回复数
	 */
	public  void calcPostNum(int forumid) {
		try {
			new ForumDao().calcPostNum(forumid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 计算指定论坛的最后主题
	 */
	public  void calcLastTopic(int forumid) {
		try {
			new ForumDao().calcLastTopic(forumid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除一个回帖
	 * 
	 * @param forumid
	 */
	public  int delOnePost(int forumid) {
		int deleted = 0;
		try {
			deleted = new ForumDao().delOnePost(forumid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return deleted;

	}
}
